package com.manong.bookstore.store.service.impl;

import com.alibaba.nacos.client.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manong.common.utils.PageUtils;
import com.manong.common.utils.Query;

import com.manong.bookstore.store.dao.CategoryDao;
import com.manong.bookstore.store.entity.CategoryEntity;
import com.manong.bookstore.store.service.CategoryService;
//import org.springframework.util.StringUtils;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

//    @Override
//    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<CategoryEntity> page = this.page(
//                new Query<CategoryEntity>().getPage(params),
//                new QueryWrapper<CategoryEntity>()
//        );
//
//        return new PageUtils(page);
//    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //1、获取key
        String key = (String) params.get("key");
        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(key)){
            queryWrapper.eq("category_id",key)
                    .or().eq("store_id",key)
                    .or().like("category_name",key)
                    .or().like("category_desc",key);
        }

        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Integer storeId) {
        if(storeId==0){
            return queryPage(params);
        }
        String key = (String) params.get("key");
        QueryWrapper<CategoryEntity> wrapper=new QueryWrapper<CategoryEntity>().eq("store_id",storeId);
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.like("category_name", key).or().like("category_desc", key);
            });
        }
        IPage<CategoryEntity> page=this.page(new Query<CategoryEntity>().getPage(params),wrapper);
//        List<CategoryEntity> list = page.getRecords();
//        String limit= (String) params.get("limit");
//        Page pp = (Page) params.get("page");
//        if(limit!=null && pp!=null){
//            int pageSize = Integer.parseInt(params.get("limit").toString());
//            int currentPage = (int) pp.getCurrent();
//
//            int total = list.size();
//
//            page.setRecords(list.subList((currentPage - 1) * pageSize,
//                    (currentPage * pageSize) < list.size() ? (currentPage * pageSize) : list.size()));
//            page.setTotal(total);
//        }
        return new PageUtils(page);
    }

}